<template>
	<div>
		<main-content
			v-if="pageType === 'i'"
			:tableHeight.sync="tableHeight"
			:otherDependenComponents="otherDependenComponents"
		>
			<template slot="query">
				<el-form
					ref="queryForm"
					:inline="true"
					:model="queryData"
					label-width="auto"
					class="flex-warp-container"
				>
					<el-form-item
						:label="`${$t('common.belong_merchant')}:`"
						class="flex-row-container"
						label-width="77px"
					>
						<merchant-select
							v-model="queryData.merchantId"
						></merchant-select>
					</el-form-item>
					<el-form-item
						:label="`${$t('funds.fund_audit.return_water_cycle')}:`"
						class="flex-row-container"
						label-width="77px"
					>
						<el-date-picker
							v-model="queryTime"
							size="medium"
							:picker-options="pickerShortcut2"
							format="yyyy-MM-dd"
							type="daterange"
							range-separator="-"
							:start-placeholder="$t('common.start_date')"
							:end-placeholder="$t('common.end_date')"
							align="right"
							:default-time="defaultTime"
							:clearable="false"
						></el-date-picker>
					</el-form-item>
					<el-form-item
						:label="`${$t('funds.fund_audit.lock_status')}:`"
						class="flex-row-container"
						label-width="77px"
					>
						<el-select
							v-model="queryData.lockStatus"
							style="width: 200px"
							:placeholder="$t('common.select_all')"
							clearable
						>
							<el-option
								v-for="item in lockOrderType"
								:key="item.code"
								:label="item.description"
								:value="item.code"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item
						:label="
							`${$t('funds.fund_audit.belong_general_agent')}:`
						"
						class="flex-row-container"
						label-width="77px"
					>
						<el-input
							v-model="queryData.topProxyName"
							clearable
							size="medium"
							v-input="{ name: 'num_alp' }"
							style="width: 220px"
							:placeholder="$t('common.please_enter')"
						></el-input>
					</el-form-item>
					<el-form-item
						:label="`${$t('funds.order_no')}:`"
						prop="orderNo"
						class="flex-row-container"
						label-width="61px"
					>
						<el-input
							v-model="queryData.orderNo"
							clearable
							size="medium"
							style="width: 300px"
							:placeholder="$t('common.please_enter')"
							v-input="{ name: 'num_alp' }"
						></el-input>
					</el-form-item>
					<el-form-item
						:label="
							`${$t('funds.fund_audit.person_return_water')}:`
						"
						class="flex-row-container"
						label-width="77px"
					>
						<el-input
							v-model="queryData.minRebateAmount"
							size="medium"
							:placeholder="$t('common.minimum')"
							style="width: 130px"
							maxLength="15"
							v-input="{ name: 'float', num: '2' }"
							name="minRebateAmount"
							clearable
							@blur="
								handleAmount(
									queryData,
									1,
									'minRebateAmount',
									'maxRebateAmount',
									$t(
										'funds.fund_audit.minPersonRebate_warning'
									)
								)
							"
						></el-input>
						-
						<el-input
							v-model="queryData.maxRebateAmount"
							size="medium"
							:placeholder="$t('common.maximum')"
							style="width: 130px"
							maxLength="15"
							v-input="{ name: 'float', num: '2' }"
							name="maxRebateAmount"
							clearable
							@blur="
								handleAmount(
									queryData,
									2,
									'minRebateAmount',
									'maxRebateAmount',
									$t(
										'funds.fund_audit.maxPersonRebate_warning'
									)
								)
							"
						></el-input>
					</el-form-item>
					<el-form-item
						:label="`${$t('funds.fund_audit.direct_proxy')}:`"
						class="flex-row-container"
						label-width="77px"
					>
						<el-input
							v-model="queryData.parentProxyName"
							clearable
							size="medium"
							v-input="{ name: 'num_alp' }"
							style="width: 220px"
							:placeholder="$t('common.please_enter')"
						></el-input>
					</el-form-item>
					<el-form-item
						:label="`${$t('funds.user_name')}:`"
						class="flex-row-container"
						label-width="77px"
					>
						<el-input
							v-model="queryData.memberName"
							clearable
							size="medium"
							v-input="{ name: 'num_alp' }"
							style="width: 220px"
							:placeholder="$t('common.please_enter')"
						></el-input>
					</el-form-item>
					<el-form-item style="margin-left: 8px">
						<el-button
							type="primary"
							icon="el-icon-search"
							size="medium"
							@click="search"
						>
							{{ $t('common.search') }}
						</el-button>
						<el-button
							icon="el-icon-refresh-left"
							size="medium"
							@click="reset"
						>
							{{ $t('common.reset') }}
						</el-button>
					</el-form-item>
				</el-form>
				<div class="divBox">
					<p class="danger data-refresh">
						{{ $t('funds.fund_audit.data_update_time') }}：
						{{ now }}
					</p>
					<el-select
						v-model="activeRebate"
						size="mini"
						:placeholder="$t('common.please_choose')"
						@change="recordTime"
					>
						<el-option
							v-for="item in timeList"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</div>
			</template>
			<template slot="content">
				<div v-if="hasPermission('407033') || hasPermission('407034')">
					<el-tabs v-model="activeName" @tab-click="switchPage">
						<el-tab-pane
							v-if="hasPermission('407033')"
							:label="$t('funds.fund_audit.wait_audit_1')"
							name="1"
						></el-tab-pane>
						<el-tab-pane
							v-if="hasPermission('407034')"
							:label="$t('funds.fund_audit.wait_audit_2')"
							name="2"
						></el-tab-pane>
					</el-tabs>
					<el-table
						v-loading="loading"
						element-loading-spinner="el-icon-loading"
						border
						size="mini"
						class="small-size-table"
						:data="tableData"
						style="width: 100%"
						highlight-current-row
						:header-cell-style="getRowClass"
						:max-height="tableHeight"
					>
						<el-table-column
							align="center"
							:label="$t('funds.fund_audit.lock')"
							width="60"
						>
							<template slot-scope="scope">
								<el-checkbox
									v-if="hasPermission('407035')"
									v-model="scope.row.lockOrder"
									:disabled="!showLock(scope.row)"
									@change="lockOne($event, scope.row.id)"
								></el-checkbox>
							</template>
						</el-table-column>
						<el-table-column
							prop="auditStep"
							align="center"
							:label="$t('common.actions')"
							width="150"
						>
							<template slot-scope="scope">
								<el-button
									:class="!isMyAudit(scope.row) ? 'dis' : ''"
									type="primary"
									size="medium"
									@click="openDetail(scope.row)"
								>
									{{ $t('funds.fund_audit.audit') }}
								</el-button>
							</template>
						</el-table-column>

						<el-table-column
							prop="orderNo"
							align="center"
							:label="$t('funds.order_no')"
							width="280"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy
									v-if="!!scope.row.orderNo"
									:title="scope.row.orderNo"
									:copy="copy"
								>
									{{ scope.row.orderNo }}
								</Copy>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="reportDate"
							align="center"
							:label="$t('funds.fund_audit.return_water_cycle')"
							width="120"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{ getFormateDate(scope.row.reportDate) }}
							</template>
						</el-table-column>
						<el-table-column
							prop="merchantName"
							align="center"
							:label="$t('common.belong_merchant')"
							width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{
									merchantFilter(scope.row.merchantId) || '-'
								}}
							</template>
						</el-table-column>
						<el-table-column
							prop="topProxyName"
							align="center"
							min-width="190"
							:label="$t('funds.fund_audit.belong_general_agent')"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy
									v-if="scope.row.topProxyName"
									:title="scope.row.topProxyName"
									:copy="copy"
								>
									{{ scope.row.topProxyName }}
								</Copy>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="parentProxyName"
							align="center"
							:label="$t('funds.fund_audit.direct_proxy_name')"
							width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy
									v-if="scope.row.parentProxyName"
									:title="scope.row.parentProxyName"
									:copy="copy"
								>
									{{ scope.row.parentProxyName }}
								</Copy>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="memberName"
							align="center"
							:label="$t('funds.user_name')"
							width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<Copy
									v-if="scope.row.memberName"
									:title="scope.row.memberName"
									:copy="copy"
								>
									{{ scope.row.memberName }}
								</Copy>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="rebateAmount"
							align="center"
							:label="$t('funds.fund_audit.person_return_water')"
							width="150"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{
									handleNumber(
										scope.row.currency,
										scope.row.rebateAmount
									)
								}}
							</template>
						</el-table-column>
						<el-table-column
							prop="orderStatus"
							align="center"
							:label="$t('funds.fund_audit.audit_status')"
							width="180"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span
									:class="
										Number(scope.row.lockStatus) === 1
											? 'lockingRgba'
											: 'deleteRgba'
									"
								>
									{{
										typeFilter(
											scope.row.lockStatus,
											'auditStatus'
										)
									}}
								</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="lockTime"
							align="center"
							:label="$t('funds.fund_audit.lock_time')"
							min-width="220"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								<span v-if="!!scope.row.lockTime">
									{{ scope.row.lockTime }}
								</span>
								<span v-else>-</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="lockAccount"
							align="center"
							:label="$t('funds.fund_audit.lock_account')"
							width="150"
						>
							<template slot-scope="scope">
								{{ scope.row.lockAccount || '-' }}
							</template>
						</el-table-column>
						<el-table-column
							prop="createdAt"
							align="center"
							:label="$t('funds.apply_time')"
							min-width="180"
							show-overflow-tooltip
						>
							<template slot-scope="scope">
								{{ scope.row.createdAt || '-' }}
							</template>
						</el-table-column>
					</el-table>
					<el-row v-if="tableData.length">
						<el-col :span="24">
							<div class="grid-content">
								<el-checkbox
									v-if="hasPermission('407037')"
									v-model="lockedAllStatus"
									:label="$t('common.all')"
									name="type"
									:disabled="lockedAllDisabled"
									@change="lockAll"
								></el-checkbox>
								<el-button
									v-if="hasPermission('407038')"
									type="primary"
									size="medium"
									style="margin-left: 20px"
									:disabled="lockedArr.length === 0"
									@click="openBatchReviewDialog"
								>
									{{ $t('funds.fund_audit.batcha_audit') }}
								</el-button>
							</div>
						</el-col>
					</el-row>
					<!-- 分页 -->
					<el-pagination
						v-if="
							hasPermission('407033') || hasPermission('407034')
						"
						:current-page.sync="pageNum"
						class="pageValue"
						background
						layout="total, sizes,prev, pager, next, jumper"
						:page-size="pageSize"
						:page-sizes="pageSizes"
						:total="total"
						@current-change="handleCurrentChange"
						@size-change="handleSizeChange"
					></el-pagination>
				</div>
				<el-empty
					v-else
					:description="$t('funds.jurisdiction_tip')"
				></el-empty>
			</template>
		</main-content>
		<detail
			v-else
			:allow-audit="allowAudit"
			:detail-data="detailData"
			:activeName="activeName"
			@goBack="goBack"
		></detail>
		<!-- 批量审核 -->
		<el-dialog
			class="batch-review"
			:title="$t('funds.fund_audit.batcha_audit')"
			width="600px"
			height="600"
			:visible.sync="batchReviewDialog.visible"
			@close="onBatchReviewDialogClose"
		>
			<template>
				<div class="review-wrapper">
					<div class="top">
						<el-row>
							<el-col :span="12" class="title border-right">
								{{ $t('funds.fund_audit.audit_number') }}
							</el-col>
							<el-col :span="12" class="title">
								{{ $t('funds.fund_audit.audit_amount') }}
							</el-col>
							<el-col :span="12" class="border-right">
								<el-link
									type="primary"
									:underline="false"
									@click="batchReviewDialog.pageType = 'd'"
								>
									{{ batchReviewDialog.totalRecords }}
								</el-link>
							</el-col>
							<el-col :span="12" class="amounts-list">
								<div>
									<div
										v-for="item in Object.keys(
											batchReviewDialog.amounts
										)"
										:key="item"
									>
										{{
											handleNumber(
												item.currency,
												batchReviewDialog.amounts[item]
											)
										}}
									</div>
								</div>
							</el-col>
						</el-row>
					</div>

					<div
						v-if="batchReviewDialog.pageType === 'i'"
						class="bottom"
					>
						<el-form
							ref="batchAuditForm"
							:model="batchReviewDialog.formData"
							:rules="rules"
						>
							<div class="remark-wrapper">
								<div class="left">
									<span class="require">
										{{ $t('common.remark') }}:
									</span>
								</div>
								<div class="right">
									<el-form-item prop="remark">
										<el-input
											v-model.trim="
												batchReviewDialog.formData
													.remark
											"
											type="textarea"
											:rows="6"
											maxlength="500"
											show-word-limit
											:placeholder="
												$t(
													'funds.fund_audit.form_remark_placeholder'
												)
											"
											resize="none"
											v-input="{ name: 'noEmpty' }"
										></el-input>
									</el-form-item>
								</div>
							</div>
						</el-form>
					</div>

					<div v-else class="details">
						<el-table
							v-loading="batchReviewDialog.loading"
							border
							size="mini"
							height="300"
							class="small-size-table"
							:data="lockedArr"
							style="width: 100%"
							:header-cell-style="{ background: '#e6e6e6' }"
						>
							<el-table-column
								align="center"
								type="index"
								:label="$t('common.serial_number')"
							></el-table-column>
							<el-table-column
								align="center"
								:label="$t('funds.user_name')"
								prop="memberName"
							></el-table-column>
							<el-table-column
								align="center"
								:label="
									$t('funds.fund_audit.person_return_water')
								"
								prop="rebateAmount"
							>
								<template slot-scope="scope">
									{{
										handleNumber(
											scope.row.currency,
											scope.row.rebateAmount
										)
									}}
								</template>
							</el-table-column>
						</el-table>
						<div style="text-align: center">
							<el-button
								type="primary"
								size="default"
								style="width: 100px"
								@click="batchReviewDialog.pageType = 'i'"
							>
								{{ $t('common.confirm') }}
							</el-button>
						</div>
					</div>
				</div>
				<div v-if="batchReviewDialog.pageType === 'i'" slot="footer">
					<el-button
						:disabled="batchReviewDialog.loading"
						size="small"
						@click="batchReviewDialog.visible = false"
					>
						{{ $t('common.cancel') }}
					</el-button>
					<el-button
						:disabled="batchReviewDialog.loading"
						type="success"
						size="small"
						@click="openSeaMoonDialog(1)"
					>
						<i
							v-show="
								batchReviewDialog.loading &&
									batchReviewDialog.btnType === 1
							"
							class="el-icon-loading"
						></i>
						{{
							activeName === '1'
								? $t('funds.fund_audit.audit_success_1')
								: $t('funds.fund_audit.audit_success_2')
						}}
					</el-button>
					<el-button
						:disabled="batchReviewDialog.loading"
						type="danger"
						size="small"
						@click="openSeaMoonDialog(2)"
					>
						<i
							v-show="
								batchReviewDialog.loading &&
									batchReviewDialog.btnType === 2
							"
							class="el-icon-loading"
						></i>
						{{
							activeName === '1'
								? $t('funds.fund_audit.audit_fail_1')
								: $t('funds.fund_audit.audit_fail_2')
						}}
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { getUsername, getUserInfo } from '@/utils/auth'
import { Decimal } from 'decimal.js'
import { routerNames } from '@/utils/consts'
import MainContent from '@/components/MainContent/index.vue'

const end = () => {
	return dayjs()
		.endOf('day')
		.subtract(1, 'day')
}
const start = () => {
	return dayjs()
		.startOf('day')
		.subtract(1, 'day')
}
export default {
	name: routerNames.bonusReview,
	components: { detail: () => import('./components/detail'), MainContent },
	mixins: [list],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: ['reviewBtn', 'reviewTab'],
			queryData: {
				lockStatus: undefined,
				orderNo: undefined,
				userName: undefined,
				memberName: undefined,
				lockAccount: undefined,
				minRebateAmount: undefined,
				maxRebateAmount: undefined
			},
			queryTime: [start(), end()],
			allowAudit: false,
			pageType: 'i',
			detailData: {},
			now: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
			userName: '',
			activeName: '1',
			tableData: [],
			lockedAllStatus: false,
			lockedAllDisabled: false,
			lockedArr: [],
			batchReviewDialog: {
				pageType: 'i',
				visible: false,
				btnType: '',
				loading: false,
				totalRebateAmount: 0,
				totalRecords: 0,
				amounts: {},
				formData: {
					remark: undefined,
					ids: []
				}
			},
			timeList: [],
			rebateTimer: null,
			activeRebate: this.$t('funds.fund_audit.no_reload'),
			userInfo: {},
			highTable: 0
		}
	},
	computed: {
		lockOrderType() {
			return this.globalDics.lockStatus || []
		},
		orderStatusArr() {
			return this.globalDics.activityBonusAuditStatus || []
		},
		rules() {
			return {
				remark: [
					{
						required: true,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					},
					{
						min: 2,
						max: 500,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					}
				]
			}
		}
	},
	created() {
		if (this.hasPermission('407033') || this.hasPermission('407034')) {
			this.getKvconfigList()
		}
		if (this.hasPermission('407033')) {
			this.activeName = '1'
		} else if (this.hasPermission('407034')) {
			this.activeName = '2'
		} else {
			this.activeName = ''
		}
		this.userName = getUsername()
		this.userInfo = getUserInfo()
		this.otherDependenComponents = ['reviewBtn', 'reviewTab']
	},
	activated() {
		if (this.hasPermission('407033') || this.hasPermission('407034')) {
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			} else {
				clearInterval(this.rebateTimer)
			}
		}
	},
	deactivated() {
		clearInterval(this.rebateTimer)
	},
	beforeDestroy() {
		clearInterval(this.rebateTimer)
		this.rebateTimer = null
	},
	mounted() {
		// this.calHeight()
		// const _self = this
		// window.onresize = () => {
		// 	_self.calHeight()
		// }
	},
	methods: {
		calHeight() {
			this.$nextTick(() => {
				const rect = this.$refs.container?.getBoundingClientRect()
				if (rect?.height) {
					this.highTable = window.innerHeight - rect.height - 350
				}
			})
		},
		cleanBlankSpace(key, key1) {
			if (this[key][key1]) {
				this[key][key1] = (this[key][key1] + '').replace(/\s+/g, '')
			}
		},
		// type: 1 通过  2 拒绝
		openSeaMoonDialog(type) {
			this.$refs.batchAuditForm.validate((valid) => {
				if (valid) {
					this.batchReviewDialog.btnType = type
					this.handleBatchReview()
				}
			})
		},
		onAmountBlur(property) {
			if (!this.queryData[property]) return

			this.queryData[property] = new Decimal(
				new Decimal(this.queryData[property]).toFixed(
					2,
					Decimal.ROUND_DOWN
				)
			).toNumber()
			if (
				this.queryData.minRebateAmount > this.queryData.maxRebateAmount
			) {
				this.queryData.maxRebateAmount = this.queryData.minRebateAmount
			}
		},
		search() {
			this.pageNum = 1
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		getOrderStatusColor(status) {
			if ([1, 3].includes(status)) {
				return 'disableRgba'
			} else if ([2, 4].includes(Number(status))) {
				return 'lockingRgba'
			}
		},
		recordTime(val) {
			this.activeRebate = val

			if (Number(val)) {
				clearInterval(this.rebateTimer)
				this.rebateTimer = setInterval(this.loadData, val * 1000)
			} else {
				clearInterval(this.rebateTimer)
			}
		},
		getKvconfigList() {
			this.timeList = []
			let str = ''
			let arr = []
			const params = {
				k: 'funds.audit.pagerefresh.interval.config',
				pageNum: 1,
				pageSize: 10
			}
			this.$api.getkvconfigQueryList(params).then((res) => {
				if (res.code === 200 && res.data) {
					const val = res.data.records[0] || []
					if (val.v) {
						str = val.v
						arr = str.split(',').sort()
						arr.forEach((item, index) => {
							const obj = {}
							if (Number(item) === 0) {
								obj.label = this.$t(
									'funds.fund_audit.no_reload'
								)
								obj.value = this.$t(
									'funds.fund_audit.no_reload'
								)
								this.timeList.push(obj)
								this.activeRebate = this.$t(
									'funds.fund_audit.no_reload'
								)
								this.rebateTimer = null
							} else if (Number(item)) {
								obj.label =
									item + this.$t('funds.fund_audit.second')
								obj.value = item
								this.timeList.push(obj)
							}
							// if (Number(item) && index === 1) {
							// 	this.activeRebate = item
							// 	this.rebateTimer = setInterval(this.loadData, item * 1000)
							// }
						})
					}
				}
			})
		},
		getRebateCycle(date1, date2) {
			date1 = date1 && dayjs(String(date1)).format('YYYY-MM-DD')
			date2 = date2 && dayjs(String(date2)).format('YYYY-MM-DD')

			if (date1 === date2) {
				return date1 || '-'
			} else {
				return date1 && date2 ? date1 + ' - ' + date2 : '-'
			}
		},
		showLock(rowData) {
			return (
				(!!rowData.lockAccountId &&
					this.userInfo.id === +rowData.lockAccountId) ||
				(!rowData.lockAccountId &&
					this.userName === rowData.lockAccount) ||
				!rowData.lockAccount
			)
		},
		switchPage() {
			this.pageNum = 1
			this.tableData = []
			this.search()
		},
		filterOrderStatus(code, lock) {
			if (lock === 1) {
				return this.activeName === '1'
					? this.$t('funds.fund_audit.audit_success_1')
					: this.$t('funds.fund_audit.audit_success_2')
			} else {
				const item = this.orderStatusArr.find(
					(item) => item.code === String(code)
				)
				return item ? item.description : '-'
			}
		},
		loadData() {
			if (!this.activeName) return
			// this.$refs.queryForm.validate((valid) => {
			// 	if (valid) {
			// 	}
			// })
			this.loading = true
			this.lockedArr = []
			const [startTime, endTime] = this.queryTime || []
			this.queryData.userName === '' && delete this.queryData.userName
			this.queryData.lockAccount === '' &&
				delete this.queryData.lockAccount
			this.queryData.orderNo === '' && delete this.queryData.orderNo

			const params = {
				...this.getParams(this.queryData),
				qryType: Number(this.activeName),
				cycleStartDate: startTime
					? dayjs(startTime).format('YYYYMMDD')
					: undefined,
				cycleEndDate: endTime
					? dayjs(endTime).format('YYYYMMDD')
					: undefined
			}

			// const api =
			// 	this.activeName === '1'
			// 		? 'getActivityBonusFirstAuditList'
			// 		: 'getActivityBonusSecondAuditList'
			this.$api
				.getMemberRebateWaitAuditList(params)
				.then((res) => {
					this.now = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
					const { record, totalRecord } = res.data || {}
					this.tableData = record || []
					this.total = totalRecord || 0
					if (this.tableData) {
						let unableNum = 0
						this.tableData.forEach((item) => {
							item.lockOrder = Boolean(item.lockStatus)
							if (
								item.lockStatus === 1 &&
								((!!item.lockAccountId &&
									this.userInfo.id === +item.lockAccountId) ||
									(!item.lockAccountId &&
										this.userName === item.lockAccount))
							) {
								this.lockedArr.push(item)
							}
							if (
								item.lockStatus === 1 &&
								!(
									(!!item.lockAccountId &&
										this.userInfo.id ===
											+item.lockAccountId) ||
									(!item.lockAccountId &&
										this.userName === item.lockAccount)
								)
							) {
								unableNum++
							}
						})
						this.lockedAllStatus =
							this.tableData.length ===
							this.lockedArr.length + unableNum
						this.lockedAllDisabled =
							unableNum === this.tableData.length
					}
					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		isMyAudit(rowData) {
			return (
				rowData.lockStatus === 1 &&
				this.userName === rowData.lockAccount
			)
		},
		openDetail(rowData) {
			// this.allowAudit =
			// 	(!!rowData.lockAccountId &&
			// 		this.userInfo.id === +rowData.lockAccountId) ||
			// 	(!rowData.lockAccountId &&
			// 		rowData.lockAccount === this.userName)
			this.allowAudit = this.isMyAudit(rowData)
			this.detailData = Object.assign({}, rowData)
			this.pageType = 'd'
		},
		goBack() {
			this.pageType = 'i'
			this.loadData()
			// this.tableData = []
			// this.search()
			// setTimeout(() => {
			// 	this.search()
			// }, 300)
		},
		reset() {
			this.queryData = {
				lockStatus: undefined,
				orderNo: undefined,
				userName: undefined,
				lockAccount: undefined,
				minRebateAmount: undefined,
				maxRebateAmount: undefined
			}
			this.$refs.queryForm.resetFields()
			this.queryTime = [start(), end()]
			this.pageNum = 1
			this.search()
		},
		reloadTime() {
			this.queryTime = [start(), end()]
		},
		handleCurrentChange(val) {
			this.pageNum = val
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		handleSizeChange(val) {
			this.pageSize = val
			clearInterval(this.rebateTimer)
			this.rebateTimer = null
			if (Number(this.activeRebate)) {
				this.rebateTimer = setInterval(
					this.loadData,
					this.activeRebate * 1000
				)
			}
			this.loadData()
		},
		handleLockOrder(data) {
			const loading = this.$loading({
				lock: true,
				text: '',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.3)'
			})
			this.$api
				.memberRebateLockRecord(data)
				.then(() => {
					this.tableData = this.tableData.filter(() => true)
					this.$message({
						type: 'success',
						message: this.$t('common.action_success')
					})
					loading.close()
					this.search()
				})
				.catch(() => {
					this.tableData = this.tableData.filter(() => true)
					this.search()
					loading.close()
				})
		},
		lockOne(status, id) {
			const data = {
				idList: [id],
				lockStatus: status ? 1 : 0,
				lockType: 1
			}
			this.handleLockOrder(data)
		},
		lockAll(status) {
			const lockIds = []
			this.tableData.forEach((item) => {
				if (!status) {
					if (
						!item.lockAccountId ||
						+item.lockAccountId === this.userInfo.id
					) {
						lockIds.push(item.id)
					}
				} else {
					if (!item.lockAccountId) {
						lockIds.push(item.id)
					}
				}
			})
			if (lockIds.length > 0) {
				const data = {
					idList: lockIds,
					lockType: 2,
					lockStatus: status ? 1 : 0
				}
				this.handleLockOrder(data)
			}
		},
		openBatchReviewDialog() {
			this.batchReviewDialog.formData.ids = []
			if (this.lockedArr.length > 0) {
				this.batchReviewDialog.visible = true
				this.batchReviewDialog.amounts = {}
				this.batchReviewDialog.pageType = 'i'
				this.batchReviewDialog.formData.remark = undefined
				this.batchReviewDialog.totalRebateAmount = 0
				this.batchReviewDialog.totalRecords = this.lockedArr.length
				this.batchReviewDialog.merchantId = this.lockedArr[0].merchantId
				const amounts = {}
				this.lockedArr.forEach((item) => {
					this.batchReviewDialog.formData.ids.push(item.id)
					if (amounts[item.currency]) {
						amounts[item.currency] += parseFloat(item.rebateAmount)
					} else {
						amounts[item.currency] = parseFloat(item.rebateAmount)
					}
					// this.batchReviewDialog.totalRebateAmount += item.bounsAmount
				})
				this.batchReviewDialog.amounts = amounts
				this.batchReviewDialog.currency = this.lockedArr[0].currency
			}
		},
		onBatchReviewDialogClose() {
			this.$refs.batchAuditForm.resetFields()
		},
		handleBatchReview() {
			this.batchReviewDialog.loading = true
			// const params = {
			// 	auditResult: this.batchReviewDialog.btnType,
			// 	ids: this.batchReviewDialog.formData.ids,
			// 	auditDesc: this.batchReviewDialog.formData.remark,
			// 	orderStatus: this.activeName === '1' ? 2 : 4,
			// 	merchantId: this.batchReviewDialog.merchantId,
			// 	auditOperator: getUsername()
			// }
			const params = {
				ids: this.batchReviewDialog.formData.ids,
				auditStatus: this.batchReviewDialog.btnType,
				remark: this.batchReviewDialog.formData.remark
			}
			this.batchReviewDialog.loading = false
			this.$api
				.memberRebateAuditRecord(params)
				.then((res) => {
					this.$message.success(
						this.$t('funds.fund_audit.batcha_audit_success')
					)
					this.batchReviewDialog.visible = false
					this.batchReviewDialog.loading = false
					this.search()
				})
				.catch(() => {
					this.batchReviewDialog.loading = false
				})
		},
		filterCycleDate(start, end) {
			start = start || '-'
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	color: #909399;
	font-weight: 700;
	border-bottom: 1px solid #00000015;
}
::v-deep .caret-wrapper {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
.divBox {
	display: flex;
	.el-select {
		width: 100px;
	}
}
.data-refresh {
	margin-top: 0;
	padding-bottom: 20px;
	line-height: 28px;
	margin-right: 20px;
}
.grid-content {
	height: 45px;
	line-height: 45px;
	padding: 2px 22px;
	border: 1px solid #ebeef5;
}
.batch-review {
	.el-dialog__title {
		font-weight: 550;
	}
	.el-dialog__body {
		padding: 10px 20px;
	}
	.review-wrapper {
		height: auto;
		display: flex;
		flex-direction: column;
		border: 1px solid rgba(0, 0, 0, 0.164);
		border-top: 0;
		.top {
			border-bottom: 1px solid rgba(0, 0, 0, 0.164);
			.check-details {
				padding: 10px;
				font-size: 15px;
				font-weight: 600;
				span {
					font-size: 12px;
					color: #038eff;
					cursor: pointer;
				}
			}
			.el-row {
				text-align: center;
				.title {
					background-color: rgba(0, 0, 0, 0.08);
					border-top: 1px solid rgba(0, 0, 0, 0.164);
					border-bottom: 1px solid rgba(0, 0, 0, 0.164);
					font-weight: 550;
				}
				.border-right {
					border-right: 1px solid rgba(0, 0, 0, 0.164);
				}
				.el-col {
					height: 40px;
					line-height: 40px;
				}
				.amounts-list {
					border-left: 1px solid rgba(0, 0, 0, 0.164);
					height: auto;
					min-height: 40px;
					padding: 10px 0;
					line-height: 20px;
				}
			}
		}

		.bottom {
			flex: 1;
			.remark-wrapper {
				display: flex;
				height: 100%;
				.left {
					display: flex;
					flex-direction: column;
					text-align: center;
					justify-content: space-around;
					width: 100px;
					border-right: 1px solid rgba(0, 0, 0, 0.164);
				}
				.require::before {
					content: '*';
					position: relative;
					top: 2px;
					right: 2px;
					color: red;
				}
				.right {
					flex: 1;
					padding: 12px 10px 0px;
				}
			}
		}
	}
	.details {
		.top {
			border-bottom: 1px solid rgba(0, 0, 0, 0.164);
			.check-details {
				padding: 10px;
				font-size: 15px;
				font-weight: 600;
				span {
					font-size: 12px;
					color: #038eff;
					cursor: pointer;
				}
			}
			.el-row {
				text-align: center;
				.title {
					background-color: rgba(0, 0, 0, 0.08);
					border-top: 1px solid rgba(0, 0, 0, 0.164);
					border-bottom: 1px solid rgba(0, 0, 0, 0.164);
					font-weight: 550;
				}
				.border-right {
					border-right: 1px solid rgba(0, 0, 0, 0.164);
				}
				.el-col {
					height: 40px;
					line-height: 40px;
				}
			}
		}
	}
	.detail-header {
		.title {
			font-weight: 600;
			font-size: 15px;
		}
		display: flex;
		justify-content: space-between;
		border: 1px solid #ebeef5;
		align-items: center;
		padding: 10px;
	}
	::v-deep .el-table {
		margin-bottom: 10px;
	}
}
</style>
